package com.net.io.netty.serialize.jdk;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;

/**
 * @author zhangtengfei
 * @date 2022/2/16 16:44
 */
public class PerformTestUserInfo {

    public static void main(String[] args) throws IOException {
        UserInfo userInfo = new UserInfo();
        userInfo.buildUserID(100).buildUserName("Welcome to Netty.");
        int loop = 1000000;
        /*
         * -----------------------------------------------------------------------------------
         * ----------------------------------- jdk序列化耗时 -----------------------------------
         * -----------------------------------------------------------------------------------
         */
        ByteArrayOutputStream bos;
        ObjectOutputStream os;
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            bos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(bos);
            // 序列化
            os.writeObject(userInfo);
            os.flush();
            os.close();
            byte[] b = bos.toByteArray();
            bos.close();
        }
        long endTime = System.currentTimeMillis();
        // The jdk serializable cost time is  : 1211 ms
        System.out.println("The jdk serializable cost time is  : " + (endTime - startTime) + " ms");

        System.out.println("--------------------------------------------------------------------------");
        /*
         * -----------------------------------------------------------------------------------
         * ----------------------------- byte array serializable -----------------------------
         * -----------------------------------------------------------------------------------
         */
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        startTime = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            byte[] b = userInfo.codeC(buffer);
        }
        endTime = System.currentTimeMillis();
        // The byte array serializable cost time is : 99 ms
        System.out.println("The byte array serializable cost time is : " + (endTime - startTime) + " ms");
    }
}
